package com.csu.tingtian.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLDecoder;

@RestController
@RequestMapping("/alipay/")
public class AliPayController {

    @RequestMapping("{money}")
    public String ailpay(@PathVariable("money") double money) throws IOException {

        //去沙箱里面找自己的
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000117684130",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCwA9+TfOfDZBaanBwRpKNUa4xxEB4IflMFqGni35sqkEXZCZPBHc+6uayMNBs1QZHFZo2QRohmI82cK7qLP6+XljVH7vHWELvxmIvGhApF1TfaE36bNLjFLAf1vrE+O5qpBbY7nQupbnAlUaJPSwKQMrZUoNRdP6b0nvzAel5nkDFPuJAAxRDC/kMxbKLz+ErtyXYSdY48gmgpqoFrOtVyaDUNZ/7nQKS2OPM938oZ4zdt1lRhQaSm6EHCy7UjQlyOi1pCx16UEbtzU4G0DNHqqJnzaMx6hcWXbXesBa837dznBD30cq/Cuwm5ot4nMT2+b6H4MmGKEXmrAUTDp5oNAgMBAAECggEAQoSENpmqgIXPoQhD90rvypSGU6gSoYUT3PyDX5C/c/tn2hirdPzOJzPKCIYcjijEbQRPlWmCXI9FQbKo4bzkn/XNFu0vCYXzcN8EW7ZEp5VncQuS+haXiTQVogI5u1oNHbkh4C9+W753tTK/kxW210XC8AKDnuXSM3nt7NOR37LWL3jLeyn3XyoUtIsZhUVoQm68w2e2PidJ1guHTr1o3wzDvWyMg+WNnqiZyR3R4uO7LVdlfA+IUMVlaUFK76W8yNEOUvZClB4Tk27VbDnKs0bd9rq2Y//Cz0GWwKRaMT4uhDbJ92RINsp0m7SRpJmJdkHfD95UzCDuLEBvWSsJoQKBgQDpxD3TEef+K9lIz/UmsVhXeuhv1TGBQgtrk7xjKWZxgq1sqrwMJDRi08nHXiNmOuSYKx8kfU/KDO6zFSe+HR1YBB9eYLXYX/1cnii3N5CN/iP3s3EO9H0CxqIymKiaY3Z63Vpjw85HffnNsB3lrfqZnQLODRwpzIYVkvmIuiLI+QKBgQDAwX9RR9Liw0lH1bNqMEIE6zbyw6Idb2o4rXuedLflqbxYgMSh0D8l7Cs3OedhACWus8yq8zi/N6X0hppsKpK+ac1PND8iBRAqrjx8/oECriSOGSMxauOyZr02WrQynPTTw6xKMVJkfrwX3mJR0auAyvKozD01uFZ55tKup94StQKBgFucFypnQwY9gtpg/M6cA2XM+dgRsWPZUQZokBRAPjBPqM0EH8uWSJ1OKC4kOzUt8TWc+IhhP2qMJOMzcC611DEQ9IYU2k6gV3b7WDNs6A5tQQUqYi0dLPxwgVHYt4JBZkYCBxE/w9AKk2HDGgRPaPzYJig/xQCwcP91aKrnXwhpAoGAHt+fReSMKZf/58qZxxtdp64JCg89qxtKVzOqRjIJtnZeXYqadBSH2MogDeNvZQECCB5yXLtCTpZaNXVNzJ9xC6CkqtgCeK947G0l8RtOjKBZ6IysL2EggVWlXsxeanDrYfPIZ8XInaIS/Hu5IcPEhYd8IH8+Gpmg+Dt5aVIn5rUCgYA13wPzrFgf0uCK5vUm8CJwoFQOqst4iSN1O5OwIvDx7Fxh3tsqM8YG6dLSf19VYA5yFRWcbvXlt+xrjfTQIOYd+vs8XF4hPpTkmpope4pPdZOij5kbAb9hPOzpvk3jqCfTLceTmlX8ASK2I+05nZKJSPik31GvpDjKFABZ4sp2DA==",  //私钥 不知道是什么 往上面看 配置沙箱密钥的时候 自己保存的
                "json","utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA09/Upe55kXNMBM4rddF0Wy/VQJy47+JtxHKTEQA1SDGy2yGwLAc3wk1P6hAfJuDPvJLN17a/MAEzErxEvK+gPHeRxEoHO0d6t1VQHbcJR3AKb+GnvqrNY6bGFn4r2o4uNozqtrQtDiMTPyuE8P8rI1uM4nVoCXm7Cw4vNJ+gf25Xh6J5KDFTazz+9r8qexsIx8RLkbv78mBhxsLWyDdruWxwnAPel413NxKpRraChpt5BNTV2qCIAb8Nn8azDTCLEUW8xs06wGK88oCp/O68ga/l/NLoiIDLXJ5K01ISxyTmTsT99a8FBxEmi4Eu+fdBuScoUcXyUcHnca7lqFIy/QIDAQAB",  "RSA2" );

        //订单号  自定义
        String out_trade_no = "20217211614321595924072510";
        out_trade_no = URLDecoder.decode(out_trade_no,"UTF-8");
        String total_amount = String.valueOf(money);  //付款金额
        total_amount = URLDecoder.decode(total_amount,"UTF-8");
        String subject = "订单";   //标题
        subject = URLDecoder.decode(subject,"UTF-8");
        String body = "";   //描述
        body = URLDecoder.decode(body,"UTF-8");
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        //同步通知,支付成功去页面【必须外网能访问】
        alipayRequest.setReturnUrl("http://www.baidu.com");
        //异步通知,支付成功去接口处理东西【必须外网能访问】
        alipayRequest.setNotifyUrl("http://www.baidu.com");

        //配置参数
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\""+ out_trade_no +"\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":"+ total_amount +"," +
                "    \"subject\":\""+ subject +"\"," +
                "    \"body\":\""+ body +"\"" +
                "    }"+
                "  }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //form就是一个表单 html 直接给前端 替换 body标签里面的东西
        return  form;
    }
}
